<?php
/**
 * 用户分组管理
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Category;
class UserGroupController  extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=M('UserGroup');
	}
	public function index(){
		if(!$this->checkPermission('UserGroup')) $this->jump();
		$list=$this->Model->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function oper($user_group_id=0){
		if(!$user_group_id && !$this->checkPermission('AddUserGroup')) $this->jump();
		$map['user_group_id']=$user_group_id;
		$user_group=$this->Model->where($map)->find();
		if($user_group_id && empty($user_group)) $this->jump(L('error_404'));
		if($user_group) {
			$user_group['permission']=json_decode($user_group['permission'],true);
			$user_group['navigation']=json_decode($user_group['navigation'],true);
		}

		if(IS_POST){
			$user_group=I('post.');
			if($this->validateForm($user_group_id)){
				$user_group['permission']=json_encode($user_group['permission']);
				$user_group['navigation']=json_encode($user_group['navigation']);
				if($user_group_id){
					$this->Model->where($map)->save($user_group);
				}else{
					$this->Model->add($user_group);
				}
				
				session('success',$user_group_id?L('success_edit'):L('success_add'));
				$this->redirect('index');
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$permissions=D('Permission')->getPermissions();
		$menu_where=array(
			'status'	=>	1,
			'is_show'	=>1
			);
		$navigations=D('Menu')->getMenus($menu_where);
		$permissions=Category::unlimitedForLayer ($permissions,'child',0,'permission_id',"parent_id");
		$navigations=Category::unlimitedForLayer ($navigations,'child',0,'menu_id',"pid");		
		$this->assign('permissions',$permissions);
		$this->assign('navigations',$navigations);
		$this->assign('msg',$this->msg);
		$this->assign('user_group',$user_group);
		$this->display();
	}
	private function validateForm($user_group_id=0){
		if($user_group_id && !$this->checkPermission('EditUserGroup')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		if(mb_strlen($data['name'])<2 || mb_strlen($data['name'])>64){
			$this->msg['name']	=	L('error_name');
		}else{
			$map=array(
				'name'	=>	$data['name'],
				'user_group_id'	=>	array('neq',$user_group_id)
				);
			$u_group=$this->Model->where($map)->find();
			if($u_group) $this->msg['name'] = L('error_name_exist');
		}

		return !$this->msg;
	}
	public function del(){
		$ids=I('post.selected');
		$map['user_group_id']=array('in',$ids);
		if($this->validateDelete()){
			$count=$this->Model->where($map)->delete();
			session('success',L('success_delete',array('count'=>$count)));
		}else{
			session('danger',$this->msg['warning']);
		}
		$this->redirect('index');
	}
	private function validateDelete(){
		if($this->checkPermission('DeleteUserGroup')){
			$ids=I('post.selected');
			if(empty($ids)){
				$this->msg['warning']=L('error_not_seleted');
			}else{
				$map=array('user_group_id'=>array('in',$ids));
				$user=M('User')->where($map)->find();
				if($user){
					$this->msg['warning']=L('error_has_user');
				}
			}
		}else{
			$this->msg['warning']=L('error_not_permission');
		}
		return !$this->msg;
	}
}